// 没有写路径的包，默认会从node-modules这个文件夹中导入
const koa = require('koa')
const data = require('./data/local')
// 导入路由
const router = require('koa-router')()//这里等于const router = new router()
// 通过new新建一个应用
const app = new koa()
// 新建一个路由
/* router.get("/my",async ctx=>{
    ctx.body = {
        code:200,
        msg:'my'
    }
})
router.get("/firend",async ctx=>{
    ctx.body = {
        code:200,
        msg:"friend"
    }
}) */
router.get('/top/playlist',async (ctx)=>{
    console.log(ctx.request.query);
    var {offset,limit} = ctx.request.query
    offset = Number(offset)
    limit = Number(limit)
    if(!offset){
        offset = 0
    }
    if(!limit){
        limit = 5
    }
    var result = data.result
    ctx.body = {
        code:200,
        data:result.slice(offset,offset+limit),
        total:result.length
    }
})
//引用路由
app.use(router.routes())
app.listen(8000,()=>{
    console.log('服务器打开了');
})